<?php


namespace app\api\library\login\wechatLogin;


use app\api\help\Http;
use app\api\model\MallUser;
use app\api\model\MallWxapp;
use think\facade\Db;
use think\facade\Log;

class V
{
    public static function run($param)
    {
        $mobile      = _checkEmptyParam($param[ 'mobile' ], '手机号不能为空');
        $code        = _checkEmptyParam($param[ 'code' ], '验证码不能为空');
        $openId      = _checkEmptyParam($param[ 'openId' ], 'openId不能为空');
        $promotionId = $param[ 'promotionId' ];
        //查询用户最新的一次验证码
        $userCode = Db::name('mall_mobile_msg')->where([['mobile', '=', $mobile]])->order(['id' => 'desc'])->find();
        if (empty($userCode)) {
            _outPut(204, '', '请先获取验证码');
        }
        if ($userCode[ 'code' ] != $code) {
            _outPut(204, '', '验证码错误');
        }
        if ($userCode[ 'expire_time' ] < time()) {
            _outPut(204, '', '验证码已过期');
        }
        $user = Db::name('mall_user')->where('open_id', $openId)->find();
        if ( !empty($user)) {
            if ($user[ 'mobile' ] != $mobile) {
                _outPut(204, '', $user[ 'mobile' ]);
            }
        }
        $user = Db::name('mall_user')->where('mobile', $mobile)->find();
        if ( !empty($user)) {
            if ($user[ 'open_id' ] != $openId) {
                _outPut(204, '', '您的已绑定其它微信');
            }
        }
        if (empty($user)) {
            $openidInfo = Db::name('mall_openid')->where('open_id', $openId)->find();
            if ( !empty($openidInfo)) {
                $addData[ 'nick_name' ]  = $openidInfo[ 'nickname' ];
                $addData[ 'name' ]       = $openidInfo[ 'nickname' ];
                $addData[ 'avatar_url' ] = $openidInfo[ 'headimgurl' ];
            } else {
                $addData[ 'nick_name' ]  = 'ap_' . substr($mobile, -4);
                $addData[ 'name' ]       = 'ap_' . substr($mobile, -4);
                $addData[ 'avatar_url' ] = 'https://tg.aipintuangou.com/upload/20230422/avatar.png';
            }
            $addData[ 'mobile' ]      = $mobile;
            $addData[ 'open_id' ]     = $openId;
            $addData[ 'wx_mobile' ]   = $mobile;
            $addData[ 'create_time' ] = time();
            $addData[ 'update_time' ] = time();
            $userId                   = MallUser::insertGetId($addData);
            $user                     = MallUser::where('id', $userId)->find();
            //处理推广人
            if ( !empty($promotionId)) {
                if ($promotionId == $userId) {
                    Log::write('推广人不能添加自己');
                }
                $promotion = Db::name('mall_promotion')->where('user_id', $promotionId)->find();
                if ( !empty($promotion)) {
                    if ($promotion[ 'status' ] == 0) {
                        Log::write('推广人正在申请推广中');
                    }
                } else {
                    Log::write('该推广人未被申请');
                }
                $user = Db::name('mall_user')->where('id', $userId)->find();
                if ( !empty($user[ 'promotion_id' ])) {
                    Log::write('该用户已绑定推广人');
                }
                $updateData = [
                    'promotion_id'   => $promotionId,
                    'promotion_time' => time(),
                ];
                if (false === Db::name('mall_user')->where('id', $userId)->update($updateData)) {
                    Log::write('绑定推广人失败');
                }
            }
        }
        
        _output(200, $user);
    }
}